//
//  WBStatusPictureView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/16.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBStatusPictureView.h"
#import <Masonry.h>
#import "WBPictureInfo.h"
#import <UIImageView+WebCache.h>


#pragma mark - 自定义cell类
@interface WBStatusPictureViewCell: UICollectionViewCell

//给外界提供一个接口，供外界赋值
@property (nonatomic , strong) WBPictureInfo *pictureInfo;

//自定义cell的背景图片控件
@property (nonatomic , strong) UIImageView *bgImageView;

@end

@implementation WBStatusPictureViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        //设置UI
        [self setupUI];
    }
    
    return self;
    
}

#pragma mark - 重写set方法
- (void)setPictureInfo:(WBPictureInfo *)pictureInfo
{
    _pictureInfo = pictureInfo;
    

    //使用SDWebImage获取网络图片
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:pictureInfo.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"weibo_placeholder"]];

}

#pragma mark - 懒加载背景图片控件
- (UIImageView *)bgImageView
{
    if(!_bgImageView)
    {
    
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weibo_placeholder"]];
        _bgImageView.clipsToBounds = YES;
        _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    
    return _bgImageView;

}

- (void)setupUI
{
    
    self.backgroundColor = [UIColor whiteColor];
    
    //添加控件
    [self.contentView addSubview:self.bgImageView];
    
    //设置约束
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView).mas_offset(UIEdgeInsetsZero);
    }];
    
}

@end

#pragma mark - WBStatusPictureView类
@interface WBStatusPictureView ()<UICollectionViewDataSource>

//测试label 显示数据
@property (nonatomic , strong) UILabel *numLabel;

@end

@implementation WBStatusPictureView

CGFloat margin = 5;
CGFloat cellWH = 80;

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if(self = [super initWithFrame:frame collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]])
    {
        
        //设置UI
        [self setupUI];
    
    }
    
    return self;

}

- (UILabel *)numLabel
{
    if(!_numLabel)
    {
        
        _numLabel = [[UILabel alloc] init];
        
        _numLabel.textColor = [UIColor redColor];
        
        _numLabel.font = [UIFont systemFontOfSize:20];
        
    }
    
    return _numLabel;
    
}

#pragma mark - 重写数据源属性的set方法
- (void)setPictInfos:(NSArray<WBPictureInfo *> *)pictInfos
{
    _pictInfos = pictInfos;
    
    self.numLabel.text = [NSString stringWithFormat:@"%lu",pictInfos.count];
    
    CGSize cellSize = [self calculateWithNumOfCells:(int)pictInfos.count];
    
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(cellSize);
        
    }];
    
    //刷新
    [self reloadData];

}

#pragma mark - 计算collectionView的大小
- (CGSize)calculateWithNumOfCells:(int)count
{
    int col = count ==4 ? 2 : count > 3 ? 3 : count;
    
    int row = (count - 1)/3 + 1;
    
    CGFloat w = col * cellWH + (col - 1) * margin;
    CGFloat h = row * cellWH + (row - 1) * margin;
    
    return CGSizeMake(w, h);

}

#pragma mark - 设置UI
- (void)setupUI
{
    //设置背景颜色
    self.backgroundColor = [UIColor clearColor];
    
    //添加控件
    [self addSubview:self.numLabel];
    
    //设置约束
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.center.equalTo(self);
        
    }];
    
    //设置数据源代理
    self.dataSource = self;
    
    //注册cell
    [self registerClass:[WBStatusPictureViewCell class] forCellWithReuseIdentifier:@"item"];
    
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    //设置cell的大小
    layout.itemSize = CGSizeMake(80, 80);
    //设置间距的大小
    layout.minimumLineSpacing = margin;
    layout.minimumInteritemSpacing = margin;


}

#pragma mark - 数据源方法的实现
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.pictInfos.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WBStatusPictureViewCell *cell = (WBStatusPictureViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];

    cell.pictureInfo = self.pictInfos[indexPath.item];

    return cell;
}

@end





















